package com.ie.service.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.ie.common.http.ResponseResult;
import com.ie.common.security.utils.MyUtil;
import com.ie.entity.CartEntity;
import com.ie.entity.FocusEntity;
import com.ie.entity.GoodsEntity;

import com.ie.mapper.admin.GoodsMapper;
import com.ie.service.before.CartService;
import com.ie.service.before.FocusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author S2
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, GoodsEntity> implements GoodsService {
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private FocusService focusService;
    @Autowired
    private CartService cartService;

    @Override
    public ResponseResult<Map<String, Object>> getGoods(GoodsEntity goodsEntity) {
        // 为了apipost调试用，currentPage为空时，默认为1
        Integer pagenum = goodsEntity.getCurrentPage();
        if (pagenum == null || pagenum < 1) {
            goodsEntity.setCurrentPage(1);
        }
        IPage<GoodsEntity> iPage = new Page<>(goodsEntity.getCurrentPage(), 5);

        // 可以根据各种条件组合成最终条件构造成复杂的条件查询
        LambdaQueryWrapper<GoodsEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 根据商品类型查询商品
        Integer goodstypeId = goodsEntity.getGoodstypeId();
        if (goodstypeId != null && goodstypeId > 0) {
            lambdaQueryWrapper.eq(GoodsEntity::getGoodstypeId, goodstypeId);
        }
        // 根据商品名称模糊查询商品
        String gname = goodsEntity.getGname();
        if (gname != null && gname.length() > 0) {
            lambdaQueryWrapper.like(GoodsEntity::getGname, gname);
        }
        // 需要使用xml构建复杂的查询
        IPage<GoodsEntity> page = goodsMapper.iPageGoods(iPage, lambdaQueryWrapper);
        Map<String, Object> myres = new HashMap<>();
        myres.put("allGoods", page.getRecords());
        myres.put("totalPage", page.getPages());
        return ResponseResult.getSuccessResult(myres);
    }

    @Override
    public ResponseResult<List<GoodsEntity>> getAdvGoods() {
        List<GoodsEntity> listadv = lambdaQuery().eq(GoodsEntity::getIsAdvertisement, 1)
                .orderByDesc(GoodsEntity::getId)
                .last("limit 5")
                .list();
        return ResponseResult.getSuccessResult(listadv);
    }

    @Override
    public ResponseResult<List<GoodsEntity>> getGoodsIndex(GoodsEntity goodsEntity) {
        // 创建了LambadaQueryWrapper对象，并指定泛型为GoodsEntity
        LambdaQueryWrapper<GoodsEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 根据商品类型查询商品

        lambdaQueryWrapper.orderByDesc(GoodsEntity::getId).last("limit 15");
        List<GoodsEntity> list = list(lambdaQueryWrapper);
        return ResponseResult.getSuccessResult(list);
    }

    @Override
    public ResponseResult<Map<String, String>> addGoods(GoodsEntity goodsEntity) {
        byte[] logoFile = goodsEntity.getLogoFile();
        if (logoFile != null) {
            String path = "E:\\Vue_Project\\ebusiness-vue\\src\\assets";
            // Linux服务器路径
            // String path="/home/ebusiness-vue/src/assets";
            String filename = goodsEntity.getFileName();
            // 修改文件名称
            String newFileName = MyUtil.getNewFileName(filename);
            File filepath = new File(path + File.separator, newFileName);
            if (!filepath.getParentFile().exists()) {
                filepath.getParentFile().mkdirs();
            }
            try {
                FileOutputStream out = new FileOutputStream(filepath);
                out.write(logoFile);

            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            goodsEntity.setGpicture(newFileName);
        }
// 添加或者修改商品
        if (Objects.equals(goodsEntity.getAct(), "add")) {
            boolean res = save(goodsEntity);
            if (res) {
                return ResponseResult.getMessageResult(null, "A001");
            } else {
                return ResponseResult.getMessageResult(null, "A002");
            }

        } else {// 修改商品
            boolean res = updateById(goodsEntity);
            if (res) {
                return ResponseResult.getMessageResult(null, "A001");
            } else {
                return ResponseResult.getMessageResult(null, "A002");
            }

        }
    }

    @Override
    public ResponseResult<Map<String, String>> deleteGoods(GoodsEntity goodsEntity) {
        // long res1 = cartService.*
        // long res2 = focusService.*
        // long res3 = orderdetailService.*
        long res1 = 0;
        long res2 = 0;
        long res3 = 0;
        if (res1 > 0 || res2 > 0 || res3 > 0) {
            // 有关联不能删除
            return ResponseResult.getMessageResult(null, "A001");
        }
        if (removeById(goodsEntity)) {
            // 商品删除成功
            return ResponseResult.getMessageResult(null, "A002");
        } else {
            return ResponseResult.getMessageResult(null, "A003");
        }

    }

    @Override
    public ResponseResult<Map<String, Object>> iPageMyFocusGoods(GoodsEntity goodsEntity) {
        IPage<GoodsEntity> iPage = new Page<>(goodsEntity.getCurrentPage(), 5);
        // 建立基于FocusEntity的查询构造器，可以根据各种条件组合成最终条件构造成复杂的条件查询
        LambdaQueryWrapper<FocusEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(FocusEntity::getBusertableId, goodsEntity.getBusertableId());
        IPage<GoodsEntity> page = goodsMapper.iPageMyFocusGoods(iPage, lambdaQueryWrapper);
        Map<String, Object> myres = new HashMap<>();
        myres.put("allGoods", page.getRecords());
        myres.put("totalPage", page.getPages());
        return ResponseResult.getSuccessResult(myres);
    }

    @Override
    public ResponseResult<List<Map<String, Object>>> myCartGoods(CartEntity cartEntity) {
        List<Map<String, Object>> cartGoods = goodsMapper.myCartGoods(cartEntity.getBusertableId());
        return ResponseResult.getSuccessResult(cartGoods);
    }
}